<?php
session_start();

class Films extends MY_Controller{
	private $status = TRUE;
	private $token = '';
	
	function __construct(){
		parent::__construct();
		$this->load->database();
		Sys::loadLanguage($this->params['lang'],'panel');
	}

	public function _remap($method, $params){
		$res = $this->checkAccessToken();
		
		$this->load->model('user_model');
		$info = $this->user_model->getInfoUser($this->params);
		$this->params['usr_info'] = $info['user'];
		
		if($res && $this->params['usr_info']['status']==1){
			
			$this->load->model('user_model');
			$info = $this->user_model->getInfoUser($this->params);
			$this->params['usr_info'] = $info['user'];
			
			if($this->params['usr_info']['tipo']==='admin'){
				switch ($method) {
					case 'index':
							header('Location:'.Sys::$url_base.$this->params['lang_url'].'panel');
					break;
					case 'admin':
							$this->admin($method,$params);
					break;
					case 'admin_cs':
							$this->admin_cs($method,$params);
					break;
					case 'add':
							$this->add($method,$params);
					break;
					case 'save':
							$this->save();
					break;
					case 'edit':
							$this->add($method,$params);
					break;
					case 'update':
							$this->update($method,$params);
					break;
					case 'get_list_tmdb':
							$this->get_list_tmdb();
					break;
					case 'get_info_movie':
							$this->get_info_movie();
					break;
					
					case 'filmafinity':
							$this->filmafinity();
					break;
					
					default:
						show_404();
					break;
				}
			}else header('Location:'.Sys::$url_base.$this->params['lang_url'].'panel');
			
		}
		else if(isset($_COOKIE["p_goto"])){ //redireccionar a alguna url despues de logearse
			$url = $_COOKIE["p_goto"];
			setcookie('p_goto', '', time()-1200, '/');
			header("Location: ".$url);
		}else{
	    	header('Location:'.Sys::$url_base.$this->params['lang_url'].'');
		}
		
	}

	/**
	 * 	MUESTRA UN LISTADO DE LAS PELICULAS PARA PODER EDITAR LA INFORMACION
	 * 
	 * 	@param string $method 			METODO QUE LLAMA LA FUNCION
	 * 	@param array $params			PARAMETROS QUE RECIBE, PUEDEN SER SEGMENTOS DE LA URL
	 */
	public function admin($method='', $params=''){
		
		//	TITULO DE LA SECCION
		$this->params['lang_txt_seccion'] = lang('txt_seccion_films_admin');
		
		//	CARGA LOS CSS
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/films/style_admin_films.css');
		
		//	CARGA LOS JS
		$js	= array(
					array('dev/libs/jquery.js'),
					array('dev/libs/jquery.tools.min.js'),
					array('dev/panel/general.js'),
				);
		$this->carabiner->js($js);
		
		//	CARGA LA LIBRERIA DE PAGINACION Y ASIGNA LA CONFIG
		$this->load->library('pagination');
		$this->params['result_items_per_page'] = 50;
		$this->params['result_page'] = 0;
		$this->params['list'] = 'film';
		
		//	SI EXISTE EL PARAMETRO DE LA PAGINA A MOSTRAR
		if(isset($this->params['pag'])){
			if($this->params['pag'] % $this->params['result_items_per_page'] == 0)
				$this->params['result_page'] = ($this->params['pag']/$this->params['result_items_per_page']);
		}
		
		//	CARGA EL MODELO SERIES_MODEL PARA OBTENER LAS PELICULAS
		$this->load->model('publication_model');		
		$res_films = $this->publication_model->getPublications($this->params);
		
		if(is_array($res_films['publications'])){
			// $this->params['publication'] = $publication;
			$this->params['list'] = $res_films;
		}

		// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
		$this->params['selected_menu1']		= 'mnu-selected';
		$this->params['selected_submenu1']	= 'submnu-selected';
		
		$this->params['seo'] = array(
			'title' 		=> ''.Sys::$title_web,
			'description' 	=> ''
		);
		
		// $this->params['aside_extra'] = $this->load->view('interface/panel/films/aside_buttons', $this->params, true);
		$this->load->view('interface/header',$this->params);
		$this->load->view('interface/panel/content',$this->params);
		$this->load->view('interface/panel/films/admin',$this->params);
		$this->load->view('interface/panel/aside',$this->params);
		$this->load->view('interface/footer',$this->params);
	}
	/**
	 * 	MUESTRA UN LISTADO DE LAS PELICULAS QUE ESTAN COMO PROXIMOS ESTRENOS PARA PODER EDITAR LA INFORMACION
	 * 
	 * 	@param string $method 			METODO QUE LLAMA LA FUNCION
	 * 	@param array $params			PARAMETROS QUE RECIBE, PUEDEN SER SEGMENTOS DE LA URL
	 */
	public function admin_cs($method='', $params=''){
		
		//	TITULO DE LA SECCION
		$this->params['lang_txt_seccion'] = lang('txt_seccion_films_admin_cs');
		
		//	CARGA LOS CSS
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/films/style_admin_films.css');
		
		//	CARGA LOS JS
		$js	= array(
					array('dev/libs/jquery.js'),
					array('dev/libs/jquery.tools.min.js'),
					array('dev/panel/general.js'),
				);
		$this->carabiner->js($js);
		
		//	CARGA EL MODELO PUBLICATION_MODEL PARA OBTENER LOS PROXIMOS ESTRENOS
		$this->load->model('publication_model');		
		$res_films_cs = $this->publication_model->getComingSoon($this->params);
		
		if(is_array($res_films_cs['films'])){
			$this->params['list'] = $res_films_cs['films'];
		}

		// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
		$this->params['selected_menu1']		= 'mnu-selected';
		$this->params['selected_submenu0']	= 'submnu-selected';
		
		$this->params['seo'] = array(
			'title' 		=> ''.Sys::$title_web,
			'description' 	=> ''
		);
		
		// $this->params['aside_extra'] = $this->load->view('interface/panel/films/aside_buttons', $this->params, true);
		$this->load->view('interface/header',$this->params);
		$this->load->view('interface/panel/content',$this->params);
		$this->load->view('interface/panel/films/admin_cs',$this->params);
		$this->load->view('interface/panel/aside',$this->params);
		$this->load->view('interface/footer',$this->params);
	}
	
	
	/**
	 * MUESTRA EL FORMULARIO PARA AGREGAR O EDITAR PELICULAS
	 * 
	 * @param $method			Metodo que llama la funcion
	 * @param $params			Parametros del Segmento de la URL u otros
	 */
	public function add($method = '', $params=''){
		
		// CARGA LOS CSS
		$this->carabiner->css('dev/libs/superbox.css');
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/style_form.css');
		
		// CARGA LOS JS
		$js	= array(
					array('dev/libs/jquery.js'),
					array('dev/libs/superbox.jquery.js'),
					array('dev/libs/jquery.tools.min.js'),
					array('dev/panel/general.js'),
					array('dev/panel/films/films.js')
				);
		$this->carabiner->js($js);
		
		// SIRVE PARA INDICAR SI EXISTE ALGUN ERROR
		$show_error = FALSE;
		
		// SI AGREGA O GUARDA UNA PELICULA
		if($method === 'add' || $method === 'save'){
			
			// TITULO DE LA SECCION
			$this->params['lang_txt_seccion'] = lang('txt_seccion_films_add');
			
			$this->params['title'] 			= '';
			$this->params['poster']			= '';
			$this->params['date_release']	= '';
			$this->params['languaje']		= '';
			$this->params['subtitle']		= '';
			$this->params['format_film']	= '';
			$this->params['quality']		= '';
			$this->params['duration']		= '';
			$this->params['size'] 			= '';
			$this->params['synopsis_spa']	= '';
			$this->params['synopsis_eng']	= '';
			$this->params['trailers']		= '';
			$this->params['on_fb']			= '';
			$this->params['coming_soon']	= '';
			
			// SI EXISTE ALGUN MENSAJE
			if (isset($params['msg_action'])){
				$this->params['class_msg']		= 'msg-ok';
				$this->params['msg_action']	= $params['msg_action'];
			}
	
			// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
			$this->params['selected_menu1']		= 'mnu-selected';
			$this->params['selected_submenu2']	= 'submnu-selected';

			// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
			$this->params['controller'] 	= 'films/';
			$this->params['method'] 		= 'save/';
			$this->params['id']				= '';
			
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> ''
			);
		
			//	OBTIENE LOS SERVIDORES ALMACENADOS
			$this->db->select('id_servidor, nombre')->from('servidores');
			$servers = $this->db->get();
			$this->params['servers'] = $servers->result_array();
		}
		// SI SE EDITA O ACTUALIZA UNA PELICULA
		else if($method === 'edit' || $method === 'update'){
			
			// ASIGNA EL ID DE LA PELICULA
			$this->params['id_film'] = $params[0];
			
			// VALIDA LOS PARAMETROS DE LA PELICULA A EDITAR
			$response_errors = $this->validate('get');
			
			if($this->status){
				
				// CARGA EL MODEL FILMS_MODEL PARA OBTENER LA INFORMACION DE LA PELICULA A EDITAR
				$this->load->model('films_model');
				$info_movie = $this->films_model->getInfoFilm($this->params);
				
				// SI EXISTE LA INFORMACION DE LA PELICULA
				if(is_array($info_movie)){
					
					// TITULO DE LA SECCION
					$this->params['lang_txt_seccion'] = lang('txt_seccion_films_edit');
					
					$this->params['title'] 			= $info_movie[0]['title'];
					$this->params['poster']			= '';//$info_movie[0]['posters'][0]['big']
					$this->params['date_release']	= $info_movie[0]['date_release'];
					$this->params['languaje']		= $info_movie[0]['language'];
					$this->params['subtitle']		= $info_movie[0]['subtitle'];
					$this->params['format_film']	= $info_movie[0]['format_film'];
					$this->params['quality']		= $info_movie[0]['quality'];
					$this->params['duration']		= $info_movie[0]['duration'];
					$this->params['size'] 			= $info_movie[0]['size'];
					$this->params['synopsis_spa']	= $info_movie[0]['synopsis_spa'];
					$this->params['synopsis_eng']	= $info_movie[0]['synopsis_eng'];
					$this->params['trailers']		= $info_movie[0]['trailers'];
					$this->params['on_fb']			= (is_null($info_movie[0]['id_facebook'])) ? '' : 'checked' ;
					$this->params['coming_soon']	= ($info_movie[0]['coming_soon']==1) ? 'checked' : '' ;
					
					$this->params['servers_film']	= isset($info_movie[0]['sources']) ? $info_movie[0]['sources'] : null;
					$this->params['cast_film']		= isset($info_movie[0]['cast']) ? $info_movie[0]['cast'] : null;
					$this->params['categories_film']= isset($info_movie[0]['categories']) ? $info_movie[0]['categories'] : null;
					
					// 	QUERY PARA OBTENER LOS SERVIDORES EXISTENTEN
					$this->db->select('id_servidor, nombre')->from('servidores');
					$servers = $this->db->get();
					$this->params['servers'] = $servers->result_array();
					
					// SI EXISTE ALGUN MENSAJE
					if (isset($params['msg_action'])){
						$this->params['class_msg']		= 'msg-ok';
						$this->params['msg_action']	= $params['msg_action'];
					}
		
					// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
					$this->params['selected_menu1']		= 'mnu-selected';
					$this->params['selected_submenu1']	= 'submnu-selected';
		
					// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
					$this->params['controller'] 	= 'films/';
					$this->params['method'] 		= 'update/';
					$this->params['id']				= $this->params['id_film'];
					
					$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> ''
					);
				}
				else{
					// SI NO LA PELICULA QUE SE DESEA EDITAR
					$show_error = TRUE;
					$this->params['msg'] = lang('txt_films_id_update_error');
				}
			}
			else{
				// SI EXISTE ALGUN ERRO DE VALIDACIO DE PARAMETROS
				$show_error = TRUE;
				$this->params['msg'] = implode(',', $response_errors);
			}			
		}

		// SI EXISTE UN ERROR LOS MUESTRA
		if($show_error){
			$this->params['seo'] = array(
					'title' 		=> ''.Sys::$title_web,
					'description' 	=> $this->params['msg']
				);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
		else{
			$this->params['method_iframe'] 		= 'get_list_tmdb';
			$this->params['show_on_fb'] 		= TRUE;
			$this->params['show_coming_soon'] 	= TRUE;
			
			$this->params['aside_extra'] = $this->load->view('interface/panel/films/aside_buttons', $this->params, true);
			$this->load->view('interface/header',$this->params);
			$this->load->view('interface/panel/content',$this->params);
			$this->load->view('interface/panel/films/form',$this->params);
			$this->load->view('interface/panel/aside',$this->params);
			$this->load->view('interface/footer',$this->params);
		}
		
	}
	
	/**
	 * GUARDA LA INFORMACION ENVIADA DEL FORMULARIO DE UNA PELICULA.
	 */
	public function save(){
		
		// VALIDA LOS PARAMETROS DE LA PELICULA A GUARDAR
		$response_errors = $this->validate('add');
		
		// SI NO EXISTEN ERRORES DE VALIDACION DE PARAMETROS
		if($this->status == TRUE){
			
			// ASIGNA EL PARAMETRO COMING_SOON
			$this->params['coming_soon'] = (isset($this->params['coming_soon']) ? $this->params['coming_soon'] : '0');
			
			// CARGA EL MODEL FILMS_MODEL PARA GUARDAR LA INFORMACION DE LA PELICULA
			$this->load->model('films_model');
			$film = $this->films_model->saveInfoFilm($this->params);
			
			if(is_array($film)){
				$this->add('save',array('msg_action'=>lang('txt_film_save_ok')));
			}
				
		}else{
			// SI EXISTEN ERRORES DE VALIDACION DE PARAMETROS
			
			$this->carabiner->css('dev/style.css');
			$this->carabiner->css('dev/panel/style.css');
			
			$js	= array(
						array('dev/libs/jquery.js'),
						array('dev/libs/jquery.tools.min.js'),
						array('dev/panel/general.js')
					);
			
			$this->carabiner->js($js);
			$this->params['msg'] = implode(',', $response_errors);
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> $this->params['msg']
			);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
	}

	/**
	 * ACTUALIZA LA INFORMACION ENVIADA DEL FORMULARIO DE UNA PELICULA.
	 * @param $method			Metodo que llama la funcion
	 * @param $params			Parametros del Segmento de la URL u otros
	 */
	public function update($method='', $params=''){
		
		// ASIGNA EL ID DE LA PELICULA
		$this->params['id_film'] = $params[0];
		
		// VALIDA LOS PARAMETROS DE LA PELICULA A ACTUALIZAR
		$response_errors = $this->validate('update');
		
		if($this->status == TRUE){
			
			// ASIGNA EL PARAMETRO COMIN_SOON	
			$this->params['coming_soon'] = (isset($this->params['coming_soon']) ? $this->params['coming_soon'] : '0');
			
			// CARGA EL MODEL FILMS_MODEL PARA ACTUALIZAR LA INFORMACION DE LA PELICULA
			$this->load->model('films_model');
			$film = $this->films_model->updateInfoFilm($this->params);
			
			if(is_array($film)){
				$this->add('update',array($this->params['id_film'], 'msg_action'=>lang('txt_film_update_ok')));
			}

		}else{
			// SI EXISTE ALGUN ERROR DE VALIDACION DE LOS PARAMETROS DE LA PELICULA A ACTUALIZAR
			$this->carabiner->css('dev/style.css');
			$this->carabiner->css('dev/panel/style.css');
			
			$js	= array(
						array('dev/libs/jquery.js'),
						array('dev/libs/jquery.tools.min.js'),
						array('dev/panel/general.js')
					);
			
			$this->carabiner->js($js);
			$this->params['msg'] = implode(',', $response_errors);
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> $this->params['msg']
			);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
	}

	/**
	 *	OBTIENE UN LISTADO DE PELICULA DE TMDB 
	 * 
	 */
	public function get_list_tmdb(){
		
		// CARGA LOS CSS
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/style_form.css');
		
		// CARGA LOS JS
		$js	= array(
					array('dev/libs/jquery.js'),
					array('dev/panel/films/iframe_search_movies.js')
				);
		$this->carabiner->js($js);
		
		$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> ''
					);
		
		// SI EXISTE EL PARAMETRO NAME_MOVIE DE LA PELICULA A BUSCAR
		if(isset($this->params['name_movie'])){
			
			// VALIDA LOS PARAMETROS PARA REALIZAR LA BUSQUEDA DE PELICULAS
			$response_errors = $this->validate('search_movies_tmdb');
			if($this->status){
				
				// CARGA ELE MODEL FILMS_MODEL PARA OBTENER EL LISTADO DE PELICULA
				$this->load->model('films_model');
				$list = $this->films_model->getMoviesListTmdb($this->params);
				
				// SI EXISTEN PELICULA(S)
				if(isset($list['movie_list'])){
					$this->params['list'] = $list['movie_list'];
				}
				// SI NO EXISTE PELICULA MUESTRA MSG
				else{
					$this->params['msg'] = $list['list'];
					$this->params['error_params'] = $this->load->view("interface/errors/page_error", $this->params, TRUE);
				}
			}
			else{
				$this->params['msg'] = implode(',', $response_errors);				
				$this->params['error_params'] = $this->load->view("interface/errors/page_error", $this->params, TRUE);
			}
		}
		$this->load->view('interface/panel/films/frame_search_get_movie',$this->params);
	}

	/**
	 *	METODO LLAMADO POR AJAX
	 * 	
	 * OBTIENE LA INFORMACION DE UNA PELICULA DE TMDB 
	 * 
	 */
	public function get_info_movie(){
		
		// VALIDA LOS PARAMETROS DE LA PELICULA A OBTENER LA INFORMACION
		$response_errors = $this->validate('get_info_tmdb');
		
		if($this->status){
			
			// CARGA EL MODELO FILMS_MODEL PARA OBTENER LA INFORMACION DE LA PELICULA
			$this->load->model('films_model');
			$info_movie = $this->films_model->getInfoMovieTmdb($this->params);
			
			// SI EXISTE INFORMACION DE LA PELICULA REALIZA EL ECHO
			if(isset($info_movie['movie_info']))
				echo json_encode($info_movie['movie_info']);
			else
				echo json_encode(array('error'=>'Vuelva a intentarlo'));
		}
		else{
			// SI EXISTE ALGUN ERROR DE VALIDACION DE LOS PARAMETROS
			$val = implode(',', $response_errors);
			echo json_encode(array('error' => $val));
		}
	}
	
	/**
     * Permite incorporar las validaciones bases que apliquen para todos los servicios.
     * Validaciones mas espesificas tendran que ser en cada servicio en particular.
     * @author gama
     */
    protected function validate($val_method){
    	if(method_exists($this, 'validate_'.$val_method)){
    		$this->load->library('MY_Form_validation');
    		return $this->{'validate_'.$val_method}();
    	}
    }
	
	private function validate_add(){
		
		$validate = new MY_Form_validation();
		$validate->setArrayValidate($this->params);

		$poster_validate = array();
		
		if(!isset($_FILES['poster']) && !isset($this->params['poster'])){
			$poster_validate = array(
					'field'	=> 'poster',
					'label'	=> 'poster',
					'rules'	=> 'required'
			);
		}
		else if(isset($_FILES['poster']) && isset($this->params['poster'])){
			if(empty($_FILES['poster']['name']) && empty($this->params['poster']))
				$poster_validate = array(
					'field'	=> 'poster',
					'label'	=> 'poster',
					'rules'	=> 'required'
				);
		}
		else if(isset($_FILES['poster']) && !isset($this->params['poster'])){
			if(empty($_FILES['poster']['name']))
				$poster_validate = array(
					'field'	=> 'poster',
					'label'	=> 'poster',
					'rules'	=> 'required'
				);	
		} 
		else if(!isset($_FILES['poster']) && isset($this->params['poster'])){
			if(empty($this->params['poster']))
				$poster_validate = array(
					'field'	=> 'poster',
					'label'	=> 'poster',
					'rules'	=> 'required'
				);	
		}

		$config	= array(
							array(
									'field'	=> 'title',
									'label'	=> 'title',
									'rules'	=> 'required|max_length[100]'
							),
							$poster_validate
							,
							array(
									'field'	=> 'date_release',
									'label'	=> 'date_release',
									'rules'	=> 'max_length[10]'
							),
							array(
									'field'	=> 'languaje',
									'label'	=> 'languaje',
									'rules'	=> 'max_length[20]'
							),
							array(
									'field'	=> 'subtitle',
									'label'	=> 'subtitle',
									'rules'	=> 'max_length[20]'
							),
							array(
									'field'	=> 'format_film',
									'label'	=> 'format_film',
									'rules'	=> 'max_length[10]'
							),
							array(
									'field'	=> 'quality',
									'label'	=> 'quality',
									'rules'	=> 'max_length[25]'
							),
							array(
									'field'	=> 'duration',
									'label'	=> 'duration',
									'rules'	=> 'max_length[30]'
							),
							array(
									'field'	=> 'size',
									'label'	=> 'size',
									'rules'	=> 'max_length[20]'
							),
							array(
									'field'	=> 'synopsis_spa',
									'label'	=> 'synopsis_spa',
									'rules'	=> ''
							),
							array(
									'field'	=> 'synopsis_eng',
									'label'	=> 'synopsis_eng',
									'rules'	=> ''
							),
							array(
									'field'	=> 'trailers',
									'label'	=> 'trailers',
									'rules'	=> 'max_length[32]'
							),
							array(
									'field'	=> 'coming_soon',
									'label'	=> 'coming_soon',
									'rules'	=> 'expression:/^(0|1)$/'
							),
							array(
									'field'	=> 'categories[]',
									'label'	=> 'categories[]',
									'rules'	=> 'is_natural_no_zero'
							),
							array(
									'field'	=> 'cast[]',
									'label'	=> 'cast[]',
									'rules'	=> 'is_natural_no_zero'
							),
							array(
									'field'	=> 'servers[]',
									'label'	=> 'servers[]',
									'rules'	=> 'is_natural_no_zero'
							),
							array(
									'field'	=> 'sources[]',
									'label'	=> 'sources[]',
									'rules'	=> 'max_length[150]'
							),
							array(
									'field'	=> 'on_fb',
									'label'	=> 'on_fb',
									'rules'	=> 'expression:/^(0|1)$/'
							)
					);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		//	VALIDA QUE LA CANTIDAD DE servers[] SEA IGUAL A LA CANTIDAD DE sources[]
		//	VALIDA QUE LAS URLS DENTRO DE $sources[] SEAN VALIDAS

		if(isset($this->params["servers"]) || isset($this->params['sources']) || isset($this->params['online'])){
			
			$is_servers = (isset($this->params['servers'])) ? TRUE : FALSE ;
			$is_sources = (isset($this->params['sources'])) ? TRUE : FALSE ;
			$is_online 	= (isset($this->params['online'])) 	? TRUE : FALSE ;
		
			if( $is_servers && $is_sources){
	    		if(count($this->params["servers"]) != count($this->params["sources"])){
	    			$this->status = FALSE;
	    		}
				else if(count($this->params["servers"]) != count($this->params["online"])){
	    			$this->status = FALSE;
	    		}
			}
			else {$this->status = FALSE;}
			
			if($this->status){
				foreach ($this->params["sources"] as $key => $itm)
					if(!sys::urlValid($itm)){
	    				$this->status = FALSE;
						$validate->_error_array['values'] = lang('txt_invalid_url','sources => "'.$itm.'"');
					}
			}
			else {$validate->_error_array['values'] = lang('txt_missing_params','servers[]','sources[]','online[]');}
		}

		//	VALIDA QUE LA URL DEL $PARAMS['POSTER'] SEA VALIDA.
		if(isset($this->params["poster"]) && !empty($this->params['poster'])){
			$url = $this->params['poster'];
			if(!sys::urlValid($url)){
    			$this->status = FALSE;
    			$validate->_error_array['values'] = lang('txt_invalid_url', 'poster => "'.$url.'"');
			}
    	}
		
		//	VALIDA QUE LA URL DEL $PARAMS['SCREENSHOTS'] SEA VALIDA.
		if(isset($this->params["screenshots"]) && !empty($this->params['screenshots'])){
			$url = $this->params['screenshots'];
			if(!sys::urlValid($url)){
    			$this->status = FALSE;
    			$validate->_error_array['values'] = lang('txt_invalid_url', 'screenshots => "'.$url.'"');
			}
    	}
		
		$fecha = new Fecha();
		if(isset($this->params['date_release']) && !empty($this->params['date_release'])){
			if(!$fecha->is_date($this->params['date_release'])){
				$this->status = FALSE;
				$validate->_error_array['date_release'] = lang('txt_invalid_date').' (date_release)';
			}
		}
		
		return $validate->_error_array;
	}

	private function validate_get(){
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate($this->params);
		
		$config = array(
							array(
								'field' => 'id_film',
								'label' => 'id_film',
								'rules' => 'required|is_natural_no_zero'
							)
		);
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
		
	}
	
	private function validate_update(){
		
		$validate = new MY_Form_validation();
		$validate->setArrayValidate($this->params);

		$config	= array(
							array(
									'field'	=> 'id_film',
									'label'	=> 'id_film',
									'rules'	=> 'required|is_natural_no_zero'
							),array(
									'field'	=> 'title',
									'label'	=> 'title',
									'rules'	=> 'required|max_length[100]'
							),
							array(
									'field'	=> 'date_release',
									'label'	=> 'date_release',
									'rules'	=> 'max_length[10]'
							),
							array(
									'field'	=> 'languaje',
									'label'	=> 'languaje',
									'rules'	=> 'max_length[20]'
							),
							array(
									'field'	=> 'subtitle',
									'label'	=> 'subtitle',
									'rules'	=> 'max_length[20]'
							),
							array(
									'field'	=> 'format_film',
									'label'	=> 'format_film',
									'rules'	=> 'max_length[10]'
							),
							array(
									'field'	=> 'quality',
									'label'	=> 'quality',
									'rules'	=> 'max_length[25]'
							),
							array(
									'field'	=> 'duration',
									'label'	=> 'duration',
									'rules'	=> 'max_length[30]'
							),
							array(
									'field'	=> 'size',
									'label'	=> 'size',
									'rules'	=> 'max_length[20]'
							),
							array(
									'field'	=> 'synopsis_spa',
									'label'	=> 'synopsis_spa',
									'rules'	=> ''
							),
							array(
									'field'	=> 'synopsis_eng',
									'label'	=> 'synopsis_eng',
									'rules'	=> ''
							),
							array(
									'field'	=> 'trailers',
									'label'	=> 'trailers',
									'rules'	=> 'max_length[32]'
							),
							array(
									'field'	=> 'coming_soon',
									'label'	=> 'coming_soon',
									'rules'	=> 'expression:/^(0|1)$/'
							),
							array(
									'field'	=> 'categories[]',
									'label'	=> 'categories[]',
									'rules'	=> 'is_natural_no_zero'
							),
							array(
									'field'	=> 'cast[]',
									'label'	=> 'cast[]',
									'rules'	=> 'is_natural_no_zero'
							),
							array(
									'field'	=> 'servers[]',
									'label'	=> 'servers[]',
									'rules'	=> 'is_natural_no_zero'
							),
							array(
									'field'	=> 'sources[]',
									'label'	=> 'sources[]',
									'rules'	=> 'max_length[150]'
							),
							array(
									'field'	=> 'on_fb',
									'label'	=> 'on_fb',
									'rules'	=> 'expression:/^(0|1)$/'
							)
							
					);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		//	VALIDA QUE LA CANTIDAD DE servers[] SEA IGUAL A LA CANTIDAD DE sources[]
		//	VALIDA QUE LAS URLS DENTRO DE $sources[] SEAN VALIDAS		
		if(isset($this->params["servers"]) || isset($this->params['sources']) || isset($this->params['online'])){
			
			$is_servers = (isset($this->params['servers'])) ? TRUE : FALSE ;
			$is_sources = (isset($this->params['sources'])) ? TRUE : FALSE ;
			$is_online 	= (isset($this->params['online'])) 	? TRUE : FALSE ;
		
			if( $is_servers && $is_sources){
	    		if(count($this->params["servers"]) != count($this->params["sources"])){
	    			$this->status = FALSE;
	    		}
				else if(count($this->params["servers"]) != count($this->params["online"])){
	    			$this->status = FALSE;
	    		}
			}
			else {$this->status = FALSE;}
			
			if($this->status){
				foreach ($this->params["sources"] as $key => $itm)
					if(!sys::urlValid($itm)){
	    				$this->status = FALSE;
						$validate->_error_array['values'] = lang('txt_invalid_url','sources => "'.$itm.'"');
					}
			}
			else {$validate->_error_array['values'] = lang('txt_missing_params','servers[]','sources[]');}
		}
		
		//	VALIDA QUE LA URL DEL $PARAMS['POSTER'] SEA VALIDA.
		if(isset($this->params["poster"]) && !empty($this->params['poster'])){
			$url = $this->params['poster'];
			if(!sys::urlValid($url)){
    			$this->status = FALSE;
    			$validate->_error_array['values'] = lang('txt_invalid_url', 'poster => "'.$url.'"');
			}
    	}
		
		//	VALIDA QUE LA FECHA DATE_RELEASE SEA VALIDA.
		$fecha = new Fecha();
		if(isset($this->params['date_release']) && !empty($this->params['date_release'])){
			if(!$fecha->is_date($this->params['date_release'])){
				$this->status = FALSE;
				$validate->_error_array['date_release'] = lang('txt_invalid_date').' (date_release)';
			}
		}
		
		return $validate->_error_array;
	}	
	
	
	private function validate_search_movies_tmdb(){
		
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate($this->params);
		
		$config	= array(
							array(
									'field' => 'name_movie',
									'label'	=> 'name_movie',
									'rules'	=> 'required'
							)	
					);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		//	VALIDA QUE LA FECHA YEAR SEA VALIDA.
		$fecha = new Fecha();
		if(!empty($this->params['year'])){
			if(!$fecha->is_date($this->params['year'])){
				$this->status = FALSE;
				$validate->_error_array['year'] = lang('txt_invalid_date').' (year)';
			}
		}
		
		return $validate->_error_array;
	}
	
	
	private function validate_get_info_tmdb(){
		
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate($this->params);
		
		$config	= array(
							array(
									'field' => 'id_tmdb',
									'label'	=> 'id_tmdb',
									'rules'	=> 'required|is_natural_no_zero'
							),
							array(
									'field' => 'idiom',
									'label'	=> 'idiom',
									'rules'	=> 'max_length[2]'
							)
							
					);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		//	VALIDA EL LENGUAJE EN CASO DE HABERLO ESPECIFICADO
		if(!empty($this->params['idiom'])){
			if(!in_array($this->params['idiom'], array('en','es','fr'))){
				$this->status = FALSE;
				$validate->_error_array['idiom'] = lang('txt_invalid_lang_info',$this->params['idiom']);
			}
		}
		
		return $validate->_error_array;
	}
	
	
}